package com.baseframe.create1.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.baseframe.create1.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jianghejie on 15/11/26.
 */
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
    public int[] imgs = {R.mipmap.ball, R.mipmap.lottery_icon_eleven_series,
            R.mipmap.shuangse, R.mipmap.daletou,
            R.mipmap.jingsailanqiu, R.mipmap.kuaisan,
            R.mipmap.beijingdanchang, R.mipmap.shifen, R.mipmap.fucai3d, R.mipmap.pailie3, R.mipmap.pailie5, R.mipmap.qilecai};
//    text_zhi

    public String[] titles = {"竞赛足球", "11选5", "双色球", "大乐透", "竞赛篮球", "快3系列", "北京单场", "快乐十分", "福彩3D", "排列3", "排列5", "七乐彩"};

    public void setClickCallBack(ItemClickCallBack clickCallBack) {
        this.clickCallBack = clickCallBack;
    }

    public interface ItemClickCallBack {
        void onItemClick(int pos);
    }

    public List<String> datas = null;
    private ItemClickCallBack clickCallBack;

    private Context context;

    public HomeAdapter(Context context, List<String> datas) {
        this.context = context;
        this.datas = datas;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_index_home, viewGroup, false);


        return new ViewHolder(view);
    }

    public void setData(List<String> list) {
        List<String> list2 = new ArrayList<>();
        datas.addAll(list);

        notifyDataSetChanged();
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
//        viewHolder.text_zhi.setText(datas.get(position));
        viewHolder.img_tupian.setImageResource(imgs[position]);
        viewHolder.text_zhi.setText(titles[position]);
        viewHolder.text_gettitle.setText(datas.get(position));
        if (mOnItemClickListener != null) {
            //为ItemView设置监听器
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getLayoutPosition(); // 1
                    mOnItemClickListener.onItemClick(viewHolder.itemView, position); // 2
                }
            });
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView text_zhi;


        public ImageView img_tupian;


        //那个动态获取字段啊
        public TextView text_gettitle;

        public ViewHolder(View view) {
            super(view);
            text_zhi = (TextView) view.findViewById(R.id.text_zhi);

            img_tupian = (ImageView) view.findViewById(R.id.img_tupian);
            text_gettitle = (TextView) view.findViewById(R.id.text_gettitle);


        }
    }
}





















